#!/usr/bin/php
<?php
/*
  BOT KLASOWY 1.5
  Copyright (C) 2007,2008 by uosiu (uosiekspam on server uosiu with a dot info)
  GNU GPL v3 published
  This script comes without ANY warranty of work, in state 'as is'
  Author is not responsible for any demages or data loss or any other weird effects caused by this script
*/

//---- USER EDITABLE CONFIGURATION ---
$mysql_host = 'localhost';
$mysql_user = 'uosiu';
$mysql_passwd = 'pierdole';
$mysql_db = 'bot';
$ekg_pipe = '/home/uosiu/rurka';
$zapisz_passwd = 'lo8jesteloelo';
global $zapisz_passwd;
//---- END OF USER EDITABLE CONFIGURATION

mysql_pconnect($mysql_host, $mysql_user, $mysql_passwd);
mysql_select_db($mysql_db);

$odpowiedz = mysql_query("SELECT pause FROM `config` WHERE 1"); 
$row = mysql_fetch_assoc($odpowiedz);
//--- CHECK THAT BOT ISN'T LOCKED, IF IT IS, THEN WAIT 'TILL IT WON'T

if ($row['pause'] != 0) {
  for(;;) {
    unset($row);
    unset($odpowiedz);
    $odpowiedz = mysql_query("SELECT pause FROM `config` WHERE 1"); 
    $row = mysql_fetch_assoc($odpowiedz);
    if ($row['pause'] == 0) break;
    echo "lock";
    sleep(2);
  }
}
//--- MAKE OTHER INSTANCES OF BOT LOCKED
$odpowiedz = mysql_query ("UPDATE `bot`.`config` SET `pause` = '1' WHERE `config`.`pause` = 0");
//--- GETTING NICK OF MSG SENDER
  $odpowiedz = mysql_query(sprintf("SELECT nick FROM `users` WHERE number = %d LIMIT 1", (int) $argv[1])); 
  $row = mysql_fetch_assoc($odpowiedz);
  $nadawca = $row['nick'];
function zapisany($numer_nadawcy) {
  $odpowiedz = mysql_query(sprintf("SELECT nick FROM `users` WHERE number = %d LIMIT 1", (int) $numer_nadawcy)); 
  $row = mysql_fetch_assoc($odpowiedz);
  //--- CHECKING THAT USER IS SUBSCRIBED TO SPAMLIST ;)
  if (empty($row)) {
  return false;
  }
  else {
  return true;
  }
}


//--- MSG SENDER NUMBER
$numer_nadawcy = $argv['1'];

//--- EXPLODING MSG
$tmp = explode(' ',$argv[2], 2);
$polecenie = $tmp[0];
$wartosci = $tmp[1];


function zapisani($numer_nadawcy) {
  echo $numer_nadawcy;
  $a = mysql_query("SELECT nick FROM `users` WHERE 1"); 
  $file = fopen('/home/uosiu/rurka', 'w');
  fwrite($file, "/msg $numer_nadawcy ");
  while ($b = mysql_fetch_assoc($a)) {
    fwrite($file, $b['nick'].', ');
  }
  fwrite($file, "\n");
  fclose($file);
}

function wypisz($numer_nadawcy) {
  $a = mysql_query(sprintf("DELETE FROM `users` WHERE number = %d LIMIT 1", (int) $numer_nadawcy));
  $b = mysql_fetch_assoc($a);
  $file = fopen('/home/uosiu/rurka', 'w');
  fwrite($file, "/msg ".$numer_nadawcy." Zostales/as wypisany/a z systemu wysylkowego klasy 2F\n");
  fclose($file);
}

function ping($numer_nadawcy) {
  $file = fopen('/home/uosiu/rurka', 'w');
  fwrite($file, "/msg ".$numer_nadawcy." pong\n");
  fclose($file);
}

function zapisz($numer_nadawcy, $password, $nick) {
$file = fopen('/home/uosiu/rurka' ,'w');
//---IF USER ISN'T SUBSCRIBED, THEN ADD TO DB
if (!zapisany($numer_nadawcy)) {
  global $zapisz_passwd;
  if ( $password == $zapisz_passwd) {
    fwrite($file, "/msg $numer_nadawcy Zostales/as zapisany/a do systemu wysylkowego klasy 2F. Witamy!\n");
    $a = mysql_query("INSERT INTO `bot`.`users` (`ID`, `number`, `nick`, `pause`) VALUES ( NULL, '$numer_nadawcy', '$nick', '0')");
  }
  else {
    fwrite($file, "/msg $numer_nadawcy ...\n");
  }
}
//--- ELSE NOTIFY, THAT (S)HE IS ALREADY SUBSCRIBED
else {
  fwrite($file, "/msg $numer_nadawcy Juz jestes zapisany/a!\n");
}
fclose($file);
}

function msg($numer_nadawcy, $nadawca, $tresc) {
$file = fopen('/home/uosiu/rurka' ,'w');
//--- GET LIST OF GADU-GADU NUMBERS OF ACTIVE SUBSCRIBED USERS
//  $a = mysql_query(sprintf("SELECT number FROM `users` WHERE pause = '0' AND number != '%d'"), (int) $numer_nadawcy); 
  $a = mysql_query(sprintf("SELECT number FROM `users` WHERE pause = '0' AND number != '$numer_nadawcy'", (int) $numer_nadawcy));  

//--- AND SEND A MESSAGE TO EACH OF THEM
  while ($b = mysql_fetch_assoc($a)) {
//    if ($numer_nadawcy != $b['number']) {
      fwrite($file, "/msg ".$b['number']." <".$nadawca."> $tresc\n");
      sleep(1);
//    }
  }
fclose($file);
//}
}
function pause($numer_nadawcy) {
  $file = fopen('/home/uosiu/rurka' ,'w');
  $odpowiedz = mysql_query(sprintf("SELECT pause FROM `users` WHERE number = %d LIMIT 1", (int) $numer_nadawcy)); 
  $row = mysql_fetch_assoc($odpowiedz);
  if ($row['pause'] == '0') {
    $odpowiedz = mysql_query(sprintf("UPDATE `bot`.`users` SET `pause` = '1' WHERE number = %d LIMIT 1", (int) $numer_nadawcy)); 
    fwrite($file, "/msg ".$numer_nadawcy." zapauzowano\n");
  }
  else {
    $odpowiedz = mysql_query(sprintf("UPDATE `bot`.`users` SET `pause` = '0' WHERE number = %d LIMIT 1", (int) $numer_nadawcy)); 
    fwrite($file, "/msg ".$numer_nadawcy." odpauzowano\n");
  }
fclose($file);
}
//--- PROCESSING MSG
switch ($polecenie) {
case "zapisz":
    $cos = explode(' ', $wartosci, 2);
    //zapisz($numer_nadawcy, password, nick)
    zapisz($numer_nadawcy, $cos[0], $cos[1]);
    break;
case "wypisz":
    wypisz($numer_nadawcy);
    break;
case "zapisani":
    if ( zapisany($numer_nadawcy) ) {
      zapisani($numer_nadawcy);
    }
    break;
case "msg":
    if ( zapisany($numer_nadawcy) ) {
      msg($numer_nadawcy, $nadawca, $wartosci);
    }
    break;
case "ping":
    if ( zapisany($numer_nadawcy) ) {
      ping($numer_nadawcy);
    }
    break;
case "pause" :
    if ( zapisany($numer_nadawcy) ) {
      pause($numer_nadawcy);
    }
    break;
default:
  if ( zapisany($numer_nadawcy) ) {
    msg($numer_nadawcy, $nadawca, $argv[2]);
  }
  else {
    $file = fopen('/home/uosiu/rurka' ,'w');
    fwrite($file, "/msg ".$numer_nadawcy." Nie masz uprawnien do pracy w tym systemie\n");
  }
  break;
}
$odpowiedz = mysql_query ("UPDATE `bot`.`config` SET `pause` = '0' WHERE `config`.`pause` = 1");
?>

